package 校招笔试.微众笔试;

import org.junit.Test;

import java.io.BufferedInputStream;
import java.util.*;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author aodre , QQ : 480029069
 * @date 2023/4/12 18:40
 */
public class Main {

    /*
    3
    2 3 8 62
    2 4 2 3
    3 5 20 61
     */

    /*
    10
    1
    7
     */


    // 真他妈 奇葩， 只 AC  18%  ， 讲道理 这题还真的 挺有意思的！
    static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {
        int t = sc.nextInt();
        while(t > 0){
            int l = sc.nextInt();
            int r = sc.nextInt();
            int x =sc.nextInt();
            int y = sc.nextInt();
            String binx = Integer.toBinaryString(x);
            String biny = Integer.toBinaryString(y);
            int lex = binx.length(), ley = biny.length();
            if(lex > ley){
                System.out.println(0);
                continue;
            }
            int len = ley - lex;
            int count = 0,i;
            for( i = 0;i < len;i++){
                if(biny.charAt(i) == '1'){
                    count++;
                }
            }
            int ans = 0, tempI = i;
            boolean flag = true;
            for(;i < biny.length();i++){
                if(biny.charAt(i) == '1' && binx.charAt(i - tempI) == '0'){
                    count++;
                }
                else if(biny.charAt(i) == '0' && binx.charAt(i - tempI) == '1'){
                    flag = false;
                    break;
                }
            }
            l = l - Integer.bitCount(x);
            r = r - Integer.bitCount(x);
            if(flag && count >= l){
                int min = Math.min(count,r);
                for(int j = l;j <= min;j++){
                    ans += cal(count,j);
                }
            }
            System.out.println(ans);
            t--;
        }

    }

    public static int cal(int down,int up){
        int downAns = 1, upAns = 1;
        for(int i = 1; i <= up;i++){
            upAns *= i;
        }
        int temp = down;
        for (int i = up; i > 0 ; i--) {
            downAns *=  temp;
            temp--;
        }
        return downAns / upAns;
    }





    @Test
    public void test(){
//        String s = "10010";
//        StringBuilder sby = new StringBuilder();
//        for(int i = 0;i < 32 - s.length();i++){
//            sby.append('0');
//        }
//        sby.append(s);
//        System.out.println(sby);
//        System.out.println(sby.length());
//
//        System.out.println('0' + '1' == 97);
        System.out.println(cal(5, 4));
        System.out.println(cal(1, 1));

    }

    @Test
    public void test1(){
//        MyInterface.print();
//        BufferedInputStream
        HashMap<Integer,Integer> map = new HashMap<>();
        Integer integer = map.get(2);
        System.out.println(integer);


        Map<String,Integer> map1  = new LinkedHashMap<>();
        map1.put("hel",1);
        map1.put("ael0",2);
        map1.forEach((x,y) -> {
            System.out.println(x + " " + y);
//            LinkedList
        });
    }


}

interface MyInterface{

    int retu();


    static void print(){
        System.out.println("print is running!");
    }

    default void sout(){
        System.out.println(1111);
    }


}

interface MyInterface1{

     default void sout(){
        System.out.println("print1 is running!");
    }
}



class MyClass implements MyInterface , MyInterface1{
    @Override
    public int retu() {
        return 0;
    }

    @Override
    public void sout() {
        MyInterface.super.sout();
    }

      protected  <T> T function(){


//          ArrayList
//          Collections.synchronizedList();
//          ReentrantReadWriteLock
        T t = null;
        return t;
    }


    abstract class M{

        public abstract void test();


      abstract  int test1();


    }


}